package example.com.zcq.mymj.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import example.com.zcq.mymj.R;
import example.com.zcq.mymj.activity.JiazhaoActivity;
import example.com.zcq.mymj.bean.JiaoZhao;

/**
 * @author zcq
 * @package example.com.zcq.mymj.Adapter
 * @fileName MyMj
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/11/23  16:45
 */
public class JiazhaoAdapter extends RecyclerView.Adapter<JiazhaoAdapter.viewHolder> {
    private Context context;
    private List<JiaoZhao.ResultBean> mContentlistBeanList;

    public JiazhaoAdapter(Context context, List<JiaoZhao.ResultBean> contentlistBeanList) {
        mContentlistBeanList = contentlistBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.jiazhao_item, viewGroup, false);
        viewHolder viewHolder = new viewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final viewHolder viewHolder, int i) {
        JiaoZhao.ResultBean contentlistBean = mContentlistBeanList.get(i);
        viewHolder.tvWen.setText(contentlistBean.getQuestion());
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.car_jz)                //加载成功之前占位图
                .error(R.drawable.car_jz)
                .fitCenter();
        Glide.with(context)
                .asBitmap()
                .apply(options)
                .load(contentlistBean.getUrl())
                .into(viewHolder.tvPic);

        viewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int postion = viewHolder.getAdapterPosition();
                JiaoZhao.ResultBean d = mContentlistBeanList.get(postion);
                context.startActivity(new Intent(context, JiazhaoActivity.class).putExtra("bean", d));

            }
        });


    }

    @Override
    public int getItemCount() {
        return mContentlistBeanList.size();
    }

    static class viewHolder extends RecyclerView.ViewHolder {
        View view;
        TextView tvWen;
        ImageView tvPic;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            this.view = itemView;
            this.tvPic = (ImageView) view.findViewById(R.id.iv_pic);
            this.tvWen = (TextView) view.findViewById(R.id.tv_wen);
        }
    }


}
